package com.dingpeikeji.quickdeer.modules.wx.realsche.web;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealsche;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.SysAds;
import com.dingpeikeji.quickdeer.core.modules.sys.service.SysAdsService;
import com.dingpeikeji.quickdeer.modules.realsche.service.FRealscheService;

/**
 * 微信端班次车订票
 */
@Controller
@RequestMapping("${wxPath}/pu/realsche")
public class WxRealscheController extends BaseController{
	
	@Autowired
	private FRealscheService fRealscheService;
	@Autowired
	private SysAdsService sysAdsService;
	
	/**
	 * 实际班次查询条件
	 * @return
	 */
	@RequestMapping("query")
	public String query(String boardaddr, String boardaddrName, String offaddr, String offaddrName, String departdateStr, Model model) throws ParseException{
		/*====================================================*\
		|START...
		|
		| 轮播图
		|
		\*====================================================*/
		SysAds sysAdsQuery = new SysAds();
		// 微信的轮播图
		sysAdsQuery.setPosition(2);
		// 排序
		Page<SysAds> page = new Page<SysAds>();
		page.setOrderBy("numsort");
		List<SysAds> sysAdsList =  sysAdsService.findList(sysAdsQuery);
		SysAds sysAdsFirst = null; // 第一张图
		SysAds sysAdsLast = null; // 最后一张图
		if(sysAdsList != null && !sysAdsList.isEmpty()) {
			sysAdsFirst = sysAdsList.get(0);
			sysAdsLast = sysAdsList.get(sysAdsList.size()-1);
		}
		model.addAttribute("sysAdsFirst", sysAdsFirst);
		model.addAttribute("sysAdsLast", sysAdsLast);
		model.addAttribute("sysAdsList", sysAdsList);
		/*====================================================*\
		|END!
		\*====================================================*/
		
		
		Calendar calendar = Calendar.getInstance();
		model.addAttribute("oldDateStr", MessageFormat.format("new Date({0},{1},{2})",""+calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)));
		calendar.add(Calendar.DAY_OF_YEAR, +1);
		model.addAttribute("today", new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())); // 第二天日期字符串
		
		model.addAttribute("boardaddr", boardaddr);
		model.addAttribute("boardaddrName", boardaddrName);
		model.addAttribute("offaddr", offaddr);
		model.addAttribute("offaddrName", offaddrName);
		model.addAttribute("departdateStr", departdateStr);
		return "wx/modules/realsche/realscheListQuery";
	}
	@RequestMapping(value="list",method=RequestMethod.POST)
	@ResponseBody
	public Page<TRealsche> listJson(TRealsche realsche, Model model, HttpServletRequest request, HttpServletResponse response) {
		return fRealscheService.findPage(new Page<TRealsche>(request, response), realsche);
	}
	
}
